package csc.daonjpa.java.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Entity class for Account table. This is a simple POJO class with annotations
 * to define mapping with DB table
 * 
 * @author nvu3
 *
 */

@Entity
@PrimaryKeyJoinColumn(referencedColumnName = "number")
public class Account extends BaseInfo implements Serializable {
	private static final long serialVersionUID = 1L;

	// @Id
	// @GeneratedValue(strategy = GenerationType.IDENTITY)
	// private Long id;

	@Column(name = "accountNumber", unique = true)
	private String accountNumber;

	@Column(name = "accountName")
	private String accountName;

	@Column(name = "accountType")
	private String accountType;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "dt_created")
	private Date createdDate;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "dt_lst_updt")
	private Date lastUpdatedDate;

	@ManyToOne()
	@JoinColumn(name = "custId")
	private Customer customer;
	
	@OneToMany(mappedBy = "srcAcc", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	private List<TransferTransaction> transactions;

	/**
	 * Required constructor
	 */
	public Account() {
		
	}

	/**
	 * Other constructor, you can define as many constructors as you want, but
	 * the constructor without parameters is required
	 * 
	 * @param accountNumber
	 * @param accountName
	 * @param accountType
	 */
	public Account(String accountNumber, String accountName,
			String accountType, Date createdDate, Date lastUpdatedDate, Customer customer) {
		super();
		this.accountNumber = accountNumber;
		this.accountName = accountName;
		this.accountType = accountType;
		this.createdDate = createdDate;
		this.lastUpdatedDate = lastUpdatedDate;
		this.customer = customer;
	}

	public Account(String accountNumber, String accountName,
			String accountType, Date createdDate, Date lastUpdatedDate) {
		this.accountNumber = accountNumber;
		this.accountName = accountName;
		this.accountType = accountType;
		this.createdDate = createdDate;
		this.lastUpdatedDate = lastUpdatedDate;
	}
	
	public Account(String accountNumber, String accountName, 
			String accountType, long amount, Date createdDate, Date lastUpdatedDate, Customer customer) {
		super();
		this.accountNumber = accountNumber;
		this.accountName = accountName;
		this.accountType = accountType;
		this.availableAmount = amount;
		this.createdDate = createdDate;
		this.lastUpdatedDate = lastUpdatedDate;
		this.customer = customer;
	}

	/**
	 * @return
	 */
//	public Long getId() {
//		return id;
//	}

	/**
	 * @param id
	 */
//	public void setId(Long id) {
//		this.id = id;
//	}

	/**
	 * @return AccountNumber
	 */
	public String getAccountNumber() {
		return this.accountNumber;
	}

	/**
	 * Set accountNumber
	 * 
	 * @param accountNumber
	 */
	public void setAccountNumber(String accountNumber) {
		this.accountNumber = accountNumber;
	}

	/**
	 * @return AccountName
	 */
	public String getAccountName() {
		return this.accountName;
	}

	/**
	 * Set accountName
	 * 
	 * @param accountName
	 */
	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}

	/**
	 * @return AccountType
	 */
	public String getAccountType() {
		return this.accountType;
	}

	/**
	 * Set accountType
	 * 
	 * @param accountType
	 */
	public void setAccountType(String accountType) {
		this.accountType = accountType;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public Date getLastUpdatedDate() {
		return lastUpdatedDate;
	}

	public void setLastUpdatedDate(Date lastUpdatedDate) {
		this.lastUpdatedDate = lastUpdatedDate;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public List<TransferTransaction> getTransactions() {
		return transactions;
	}

	public void setTransactions(List<TransferTransaction> transactions) {
		this.transactions = transactions;
	}
	
	public void addTransaction(TransferTransaction transaction){
		if (transactions == null){
			transactions = new ArrayList<TransferTransaction>();
		}
		transaction.setSrcAcc(this);
		transactions.add(transaction);
	}
	
}
